<template>
    <div class="exam-container">
        <div class="filter-section">
            <div class="cards-container">
                <div
                        v-for="notice in notices"
                        :key="notice.id"
                        class="notice-card"
                        :class="getStatusClass(notice.state)"
                >
                    <div class="card-header">
                        <h3 class="card-title">{{ notice.title }}</h3>
                        <span class="card-status">{{ getStatusText(notice.state) }}</span>
                    </div>
                    <div class="card-time">
                        <i class="icon-clock"></i>
                        {{ formatTime(notice.sendTime) }}
                    </div>
                    <div class="card-content">{{ notice.content }}</div>
                    <div class="card-footer">
                        <div class="actions">
                            <button class="btn-detail" @click="toAnnoContent(notice)">查看详情</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 分页组件 -->
            <div style="justify-content: center">
                <el-pagination
                        :current-page="currentPage"
                        :page-size="pageSize"
                        :page-sizes="[10, 20, 30, 50]"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        :background="true"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                />
            </div>
        </div>
    </div>
</template>

<script>
    import request from "~/utils/request.js";
    export default {
        data() {
            return {
                // 公告列表数据
                notices: [],
                statusMap: {
                    0: {text: '未读', class: 'pending'},
                    1: {text: '已读', class: 'completed'}
                },
                userId:this.$store.state.user.id, // 用户id
                search: '', // 搜索关键字
                currentPage: 1,
                pageSize: 10,
                total: 0,
            }
        },
        activated() {
            this.load()
        },

        methods: {
            load() {
                request.get('/receive-announce/stuFindReceiveAnnouncePage',{
                    params:{
                        pageNum:this.currentPage,
                        pageSize:this.pageSize,
                        name:this.search,
                        userId:this.userId,
                    }
                }).then(res => {
                    this.notices = res.data.records;
                    this.total = res.data.total;
                })
            },
            getNoticesByType(type) {
                return this.notices.filter(notice => notice.type === type);
            },
            getStatusText(status) {
                return this.statusMap[status]?.text || '未知状态';
            },
            getStatusClass(status) {
                return this.statusMap[status]?.class || '';
            },
            formatTime(timeString) {
                return new Date(timeString).toLocaleString();
            },
            handleCurrentChange(item) {
                // 改变当前页码触发
                this.currentPage = item
                this.load()
            },
            handleSizeChange(item) {   // 改变当前每页的个数触发
                this.pageSize = item
                this.load()
            },
            toAnnoContent(notice) {
                this.$router.push({path: "/announce/annoContent", query: {announceId: notice.sendAnnounceId}})
            }
        }
    }
</script>

<style scoped>
    .exam-container {
        padding: 20px;
        height: 78vh;
        display: flex;
        flex-direction: column;
    }

    .filter-section {
        background: #fff;
        padding: 10px;
        border-radius: 8px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 16px;
        position: relative;
        height: 70vh;
    }

    /* 新增滚动容器样式 */
    .cards-container {
        flex: 1;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
        gap: 20px;
        max-width: 800px;
        margin: 0 auto;
        height: 55vh;
        width: 100%;
    }

    /* 优化卡片样式 */
    .notice-card {
        background: #fff;
        border-radius: 12px;
        padding: 20px;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        width: 100%;
        box-sizing: border-box;
        border: 1px solid rgba(0, 0, 0, 0.05);
    }

    .notice-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
    }

    /* 优化状态标签 */
    .card-status {
        font-size: 12px;
        padding: 6px 12px;
        border-radius: 20px;
        font-weight: 500;
    }

    .pending .card-status {
        background: #fff7e6;
        color: #fa8c16;
    }

    .completed .card-status {
        background: #f6ffed;
        color: #52c41a;
    }

    /* 优化文字样式 */
    .card-title {
        font-size: 17px;
        font-weight: 600;
        color: #1f2d3d;
    }

    .card-content {
        font-size: 14px;
        color: #5e6d82;
        line-height: 1.7;
        margin: 16px 0;
    }

    /* 优化时间显示 */
    .card-time {
        font-size: 13px;
        color: #99a9bf;
        margin-bottom: 12px;
    }
    .el-pagination {
        flex-shrink: 0;
        padding: 16px 0;
        background: white;
        border-radius: 0 0 8px 8px;
    }

    @media (max-width: 768px) {
        .notice-card {
            margin: 0 8px;
            padding: 16px;
        }

        .card-title {
            font-size: 15px;
        }

        .cards-container {
            gap: 16px;
        }
    }

    /* 修改分页布局 */
    .el-pagination {
        display: flex;
        justify-content: center;
        margin-top: 24px !important;
    }

    /* 样式部分保持不变 */
    .dashboard {
        max-width: 1200px;
        margin: 0 auto;
        padding: 20px;
    }

    .category-title {
        color: #333;
        border-left: 4px solid #409eff;
        padding-left: 12px;
        margin: 20px 0;
    }

    .notice-card {
        background: #fff;
        border-radius: 8px;
        padding: 16px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        transition: transform 0.3s;
        width: 100%;
        box-sizing: border-box;
    }

    .notice-card:hover {
        transform: translateY(-5px);
    }

    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 12px;
    }

    .card-title {
        font-size: 16px;
        margin: 0;
        color: #333;
    }

    .card-status {
        font-size: 12px;
        padding: 4px 8px;
        border-radius: 4px;
    }

    .card-time {
        font-size: 12px;
        color: #999;
        margin-bottom: 10px;
        display: flex;
        align-items: center;
    }

    .icon-clock {
        display: inline-block;
        width: 14px;
        height: 14px;
        background: url('data:image/svg+xml,...') no-repeat;
        margin-right: 4px;
    }

    .card-content {
        /* 保留原有基础样式 */
        font-size: 14px;
        color: #5e6d82;
        line-height: 1.7;
        margin: 16px 0;

        /* 多行省略核心代码 */
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1; /* 控制显示行数 */
        overflow: hidden;

        /* 增加备用方案 */
        max-height: calc(1.7em * 3); /* 根据行高×行数计算 */
        word-break: break-all; /* 处理长单词换行 */
    }
    .tag {
        font-size: 12px;
        padding: 4px 8px;
        background: #f0f2f5;
        border-radius: 4px;
    }
    .card-footer {
        display: flex;
        justify-content: flex-end; /* 将内容对齐到右侧 */
        align-items: center;
        border-top: 1px solid #eee;
        padding-top: 12px;
    }
    .btn-detail {
        background: #409eff;
        color: white;
        border: none;
        padding: 6px 12px;
        border-radius: 4px;
        cursor: pointer;
        transition: opacity 0.3s;
    }

    .btn-detail:hover {
        opacity: 0.8;
    }

    /* 新增滚动条样式 */
    .cards-container::-webkit-scrollbar {
        width: 8px; /* 竖向滚动条宽度 */
        height: 8px; /* 横向滚动条高度 */
    }

    .cards-container::-webkit-scrollbar-thumb {
        background-color: rgba(144, 147, 153, 0.3);
        border-radius: 8px;
        transition: background-color 0.2s;
    }

    .cards-container::-webkit-scrollbar-thumb:hover {
        background-color: rgba(144, 147, 153, 0.5);
    }

    .cards-container::-webkit-scrollbar-track {
        background: rgba(240, 240, 240, 0.5);
        border-radius: 8px;
    }

    /* Firefox兼容方案 */
    .cards-container {
        scrollbar-width: thin;
        scrollbar-color: rgba(144, 147, 153, 0.3) rgba(240, 240, 240, 0.5);
    }

    /* 优化滚动容器样式 */
    .cards-container {
        scroll-behavior: smooth; /* 平滑滚动 */
        padding-right: 4px; /* 防止内容被滚动条遮挡 */
    }

    /* 修改分页布局 */
    .el-pagination {
        display: flex;
        justify-content: center;
        margin-top: 10px !important;
    }

    /* 悬停时显示明显滚动条 */
    @media (hover: hover) {
        .cards-container {
            scrollbar-color: transparent transparent;
        }

        .cards-container:hover {
            scrollbar-color: rgba(144, 147, 153, 0.3) rgba(240, 240, 240, 0.5);
        }
    }

    /* 移动端隐藏滚动条 */
    @media (max-width: 768px) {
        .cards-container::-webkit-scrollbar {
            display: none;
        }
        .cards-container {
            -ms-overflow-style: none;  /* IE and Edge */
            scrollbar-width: none;  /* Firefox */
        }

        /* 分页样式 */
        .el-pagination {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
        }

        .el-pagination__total {
            order: 1;
            width: 50%;
            text-align: left;
        }

        .el-pagination__sizes {
            order: 2;
            width: 50%;
            text-align: right;
        }

        .el-pagination .btn-prev,
        .el-pagination .el-pager,
        .el-pagination .btn-next {
            order: 3;
            width: 100%;
            text-align: center;
            margin: 10px 0;
        }

        .el-pagination .el-pagination__jump {
            order: 4;
            width: 100%;
            text-align: center;
        }
    }

</style>
